package com.yu.mall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yu.mall.dto.BaseDTO;
import com.yu.mall.dto.PageDTO;
import com.yu.mall.dto.order.FreightDTO;
import com.yu.mall.dto.order.OrderDTO;
import com.yu.mall.dto.order.OrderQueryDTO;
import com.yu.mall.entity.Order;
import com.yu.mall.entity.OrderItem;
import com.yu.mall.exception.ServiceException;
import com.yu.mall.mapper.OrderItemMapper;
import com.yu.mall.mapper.OrderMapper;
import com.yu.mall.service.OrderService;
import com.yu.mall.util.JDUtils;
import com.yu.mall.util.ResultUtils;
import com.yu.mall.vo.PageResultVO;
import com.yu.mall.vo.ResultVO;
import com.yu.mall.vo.SelectVO;
import com.yu.mall.vo.order.FreightVO;
import com.yu.mall.vo.order.OrderAdminVO;
import com.yu.mall.vo.order.OrderItemVO;
import com.yu.mall.vo.order.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("order")
@Api(tags = "订单管理",value = "订单管理")
public class OrderController {

    @Autowired
    private OrderService orderService;
    
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @PostMapping("getFreight")
    @ApiOperation(value = "获取运费")
    public ResultVO<FreightVO>  getFreight(@RequestBody FreightDTO dto,BaseDTO baseDTO) throws ServiceException {
        dto.setOpenid(baseDTO.getOpenid());
        return orderService.getFreight(dto);
    }

    @PostMapping("crate")
    @ApiOperation(value = "创建订单")
    public ResultVO<String> crate(@RequestBody OrderDTO dto,BaseDTO baseDTO) throws ServiceException, InterruptedException {
        dto.setOpenid(baseDTO.getOpenid());
        return orderService.crate(dto);
    }

    @PostMapping("list")
    @ApiOperation(value = "查询订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value="商品名称或者订单编号"),
            @ApiImplicitParam(name = "status",value="订单状态（1 待付款，2 待收货，3 已完成 4 已取消）"),
    })
    public ResultVO<List<OrderVO>> list(PageDTO dto, String name, String status) throws ServiceException {
        return orderService.list(dto,name,status);
    }

    @GetMapping("admin/list")
    @ApiOperation(value = "后台订单列表")
    public PageResultVO<List<OrderAdminVO>> adminList(OrderQueryDTO dto) throws ServiceException {
        return orderService.adminList(dto);
    }
    
    @PostMapping("getOrderInfo")
    @ApiOperation(value = "查询订单详情")
    @ApiImplicitParam(name = "orderno",value="订单号",required=true)
    public ResultVO<HashMap<String, Object>> list(String orderno) throws ServiceException {
    	HashMap<String, Object> hashMap = new HashMap<String, Object>();
    	Order order = new Order();
    	order.setOrderNo(orderno);
    	Order selectOne = orderMapper.selectOne(order);
    	OrderItem orderItem = new OrderItem();
    	orderItem.setOrderNo(orderno);
    	List<OrderItem> select = orderItemMapper.select(orderItem);
        int num = 0;
        for (OrderItem item : select) {
            num = num + item.getNum();
        }
        selectOne.setTotalNum(num);
        selectOne.setEndTime(DateUtils.addDays(selectOne.getCreateTime(),1));
        selectOne.setCreateTime(new Date());
        hashMap.put("order", selectOne);
    	hashMap.put("orderItem", select);
        return ResultUtils.success(hashMap);
    }
    
    @PostMapping("cancelOrder")
    @ApiOperation(value = "取消订单")
    @ApiImplicitParam(name = "orderno",value="订单号",required=true)
    public ResultVO<HashMap<String, Object>> cancelOrder(String orderno) throws ServiceException {
    	Order order = new Order();
    	order.setOrderNo(orderno);
    	order = orderMapper.selectOne(order);
    	if("1".equals(order.getStatus())) {
    		String cancel = JDUtils.cancel(order.getJdOrderNo());
    		if("true".equals(cancel)) {
                order.setStatus("4");
                order.setUpdateTime(new Date());
                orderMapper.updateByPrimaryKeySelective(order);
    			return ResultUtils.success("取消成功！");
    		}else {
    			return ResultUtils.error("取消失败");
    		}
    	}
        return ResultUtils.success("取消失败！");
    }

    @GetMapping("getOrderTrack")
    @ApiOperation(value = "获取物流信息")
    @ApiImplicitParam(name = "orderNo",value="订单号",required=true)
    public ResultVO<JSONObject> getOrderTrack(String orderNo) throws ServiceException {
        String s = JDUtils.orderTrack(orderNo);
        return ResultUtils.success(JSON.parseObject(s));
    }
}
